package socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class SocketServer {

	static Scanner scanner=new Scanner(System.in);
	
	public static void main(String[] args) throws Exception {
		new Thread(new Runnable() {
			@Override
			public void run() {
				initServer(5555);
			}
		},"Server-1").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				initServer(6666);
			}
		},"Server-2").start();
	}
	
	
	public static void initServer(int port){
		try {
			ServerSocket serverSocket = new ServerSocket(port);
			Socket socket = serverSocket.accept();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			while(true){
				String string=br.readLine();
				System.out.println("Server["+port+"]读到："+string);
				String str=scanner.next();
				if(str.equals("exit")){
					break;
				}
			}
			serverSocket.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
